// CCES2012 analysis for "Policy, Politics and Public Attitudes toward the SC"
// with thanks to Joe Williams for some of the data setup code

clear

capture log using CCES_Law_2012.log, replace
set more off
use "/nfs/home/A/awhite/shared_space/gscourts/lawproject/replication/CCES12_LAW_OUTPUT.DTA", clear
svyset [pweight=V102]
cd "/nfs/home/A/awhite/shared_space/gscourts/lawproject/replication/output"

/* DEMOGRAPHICS */
gen white = race==1
gen black = race==2
gen hisp = race==3

/* KNOWLEDGE */
tab LAW311 

gen know_chief_12 = LAW373==5 if LAW373!=.
gen dk_chief_12 = LAW373==98
gen know_party_12 = LAW311==2
gen dk_party_12 = LAW311==4

egen know = rmean(know_chief_12 know_party_12)

/* ISSUES */

/* ISSUE AGREEMENT */

/* PARTIAL BIRTH ABORTION */

gen pb_agree = 1 if (LAW334==1 & LAW365==2)|(LAW334==2 & LAW365==1) //agree
replace pb_agree = -1 if (LAW334==2 & LAW365==2)|(LAW334==1 & LAW365==1) //disagree
replace pb_agree = .5 if (LAW334==1 & LAW365a==2)|(LAW334==2 & LAW365a==1) //probably agree 
replace pb_agree = -.5 if (LAW334==2 & LAW365a==1)|(LAW334==1 & LAW365a==1) //probably disagree
replace pb_agree = 0 if LAW365a==3
replace pb_agree = . if LAW334==. |LAW365==.

gen pb_notsure =  LAW365==3 if LAW365!=. //this is if you answered notsure on the FIRST question, not the followup

gen pb_agree_flat = 0 //zero unless you actively agree 
replace pb_agree_flat = 1 if pb_agree >0

gen pb_agree_01 = pb_agree 
recode pb_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

//also construct objective agreement measure (not perceived)
gen pb_realagree = 1 if LAW334==1 
replace pb_realagree = -1 if LAW334==2
replace pb_realagree = 0 if LAW334==8

gen pb_realagree_flat = pb_realagree
recode pb_realagree_flat (0 = .) (-1 = 0)

gen pb_correct = 0 if LAW365 <8
replace pb_correct = 1 if LAW365== 2
replace pb_correct = .5 if LAW365a== 2
replace pb_correct = -1 if LAW365== 1
replace pb_correct = -.5 if LAW365a== 1

gen pb_correct_flat = . //ignore whether they ever said not sure: were they right?
replace pb_correct_flat = 0 if pb_correct <0
replace pb_correct_flat = 1 if pb_correct >0

gen pb_Ryes = .
replace pb_Ryes = 0 if LAW334==1
replace pb_Ryes = 1 if LAW334==2

gen pb_SCyes = .
replace pb_SCyes = 0 if LAW365==2
replace pb_SCyes = 1 if LAW365==1
replace pb_SCyes = .75 if LAW365a ==1
replace pb_SCyes = .25 if LAW365a ==2


/* ACA Self - Mandate */

gen mandate_agree = 1 if (LAW346_1==1 & LAW366==1)|(LAW346_1==2 & LAW366==2)
replace mandate_agree = -1 if (LAW346_1==2 & LAW366==1)|(LAW346_1==1 & LAW366==2)
replace mandate_agree = .5 if (LAW346_1==1 & LAW366a==1)|(LAW346_1==2 & LAW366a==2)
replace mandate_agree = -.5 if (LAW346_1==2 & LAW366a==1)|(LAW346_1==1 & LAW366a==2)
replace mandate_agree = 0 if LAW366a==3
replace mandate_agree = . if LAW346_1==.| LAW366==.

gen mandate_notsure = LAW366==3 if LAW366!=.

gen mandate_agree_flat = 0 
replace mandate_agree_flat = 1 if mandate_agree >0

gen mandate_agree_01 = mandate_agree
recode mandate_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

gen mandate_realagree = 1 if LAW346_1==1
replace mandate_realagree = -1 if LAW346_1==2
replace mandate_realagree = 0 if LAW346_1==8

gen mandate_correct = 0 if LAW366 <8
replace mandate_correct = 1 if LAW366== 1
replace mandate_correct = .5 if LAW366a== 1
replace mandate_correct = -1 if LAW366== 2
replace mandate_correct = -.5 if LAW366a== 2
gen mandate_correct_flat = .
replace mandate_correct_flat = 0 if mandate_correct <0
replace mandate_correct_flat = 1 if mandate_correct >0

gen mandate_realagree_flat = mandate_realagree
recode mandate_realagree_flat (0 = .) (-1 = 0)

gen mandate_Ryes = .
replace mandate_Ryes = 0 if LAW346_1==2
replace mandate_Ryes = 1 if LAW346_1==1

gen mandate_SCyes = .
replace mandate_SCyes = 0 if LAW366==2
replace mandate_SCyes = 1 if LAW366==1
replace mandate_SCyes = .75 if LAW366a ==1
replace mandate_SCyes = .25 if LAW366a ==2


/* ACA Self - Medicaid */

gen medicaid_agree = 1 if (LAW346_3==1 & LAW367==1)|(LAW346_3==2 & LAW367==2)
replace medicaid_agree = -1 if (LAW346_3==2 & LAW367==1)|(LAW346_3==1 & LAW367==2)
replace medicaid_agree = .5 if (LAW346_3==1 & LAW367a==1)|(LAW346_3==2 & LAW367a==2)
replace medicaid_agree = -.5 if (LAW346_3==2 & LAW367a==1)|(LAW346_3==1 & LAW367a==2)
replace medicaid_agree = 0 if LAW367a==3
replace medicaid_agree = . if LAW346_3==.|LAW367==.

gen medicaid_notsure = LAW367 ==3 if LAW367 !=.

gen medicaid_agree_flat = 0 
replace medicaid_agree_flat = 1 if medicaid_agree >0

gen medicaid_agree_01 = medicaid_agree
recode medicaid_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

gen medicaid_realagree = 1 if LAW346_3==2
replace medicaid_realagree = -1 if LAW346_3==1
replace medicaid_realagree = 0 if LAW346_3==8

gen medicaid_correct = 0 if LAW367 <8
replace medicaid_correct = 1 if LAW367== 2
replace medicaid_correct = .5 if LAW367a== 2
replace medicaid_correct = -1 if LAW367== 1
replace medicaid_correct = -.5 if LAW367a== 1
gen medicaid_correct_flat = .
replace medicaid_correct_flat = 0 if medicaid_correct <0
replace medicaid_correct_flat = 1 if medicaid_correct >0

gen medicaid_realagree_flat = medicaid_realagree
recode medicaid_realagree_flat (0 = .) (-1 = 0)

gen medicaid_Ryes = .
replace medicaid_Ryes = 0 if LAW346_3==2
replace medicaid_Ryes = 1 if LAW346_3==1

gen medicaid_SCyes = .
replace medicaid_SCyes = 0 if LAW367==2
replace medicaid_SCyes = 1 if LAW367==1
replace medicaid_SCyes = .75 if LAW367a ==1
replace medicaid_SCyes = .25 if LAW367a ==2

/* Citizens United */

gen cu_agree = 1 if (LAW369==1 & LAW368==1)|(LAW369==2 & LAW368==2)
replace cu_agree = -1 if (LAW369==2 & LAW368==1)|(LAW369==1 & LAW368==2)
replace cu_agree = .5 if (LAW369==1 & LAW368a==1)|(LAW369==2 & LAW368a==2)
replace cu_agree = -.5 if (LAW369==2 & LAW368a==1)|(LAW369==1 & LAW368a==2)
replace cu_agree = 0 if LAW368a==3
replace cu_agree = . if LAW369==. | LAW368==.

gen cu_notsure= LAW368==3 if LAW368!=.

gen cu_agree_flat = 0 
replace cu_agree_flat = 1 if cu_agree >0

gen cu_agree_01 = cu_agree
recode cu_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

gen cu_realagree = 1 if LAW369==1
replace cu_realagree = -1 if LAW369==2
replace cu_realagree = 0 if LAW369==8

gen cu_correct = 0 if LAW368 <8
replace cu_correct = 1 if LAW368== 1
replace cu_correct = .5 if LAW368a== 1
replace cu_correct = -1 if LAW368== 2
replace cu_correct = -.5 if LAW368a== 2

gen cu_correct_flat = .
replace cu_correct_flat = 0 if cu_correct <0
replace cu_correct_flat = 1 if cu_correct >0

gen cu_realagree_flat = cu_realagree
recode cu_realagree_flat (0 = .) (-1 = 0)

gen cu_Ryes = .
replace cu_Ryes = 0 if LAW369==2
replace cu_Ryes = 1 if LAW369==1

gen cu_SCyes = .
replace cu_SCyes = 0 if LAW368==2
replace cu_SCyes = 1 if LAW368==1
replace cu_SCyes = .75 if LAW368a ==1
replace cu_SCyes = .25 if LAW368a ==2

/* Desegregation */

gen deseg_agree = 1 if (LAW362==1 & LAW361==1)|(LAW362==2 & LAW361==2)
replace deseg_agree = -1 if (LAW362==2 & LAW361==1)|(LAW362==1 & LAW361==2)
replace deseg_agree = .5 if (LAW362==1 & LAW361a==1)|(LAW362==2 & LAW361a==2)
replace deseg_agree = -.5 if (LAW362==2 & LAW361a==1)|(LAW362==1 & LAW361a==2)
replace deseg_agree = 0 if LAW361a==3
replace deseg_agree = . if LAW362==. | LAW361==.

gen deseg_notsure=LAW361==3 if LAW361 !=.

gen deseg_agree_flat = 0 
replace deseg_agree_flat = 1 if deseg_agree >0

gen deseg_agree_01 = deseg_agree
recode deseg_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

gen deseg_realagree = 1 if LAW362==2
replace deseg_realagree = -1 if LAW362==1
replace deseg_realagree = 0 if LAW362==8

gen deseg_correct = 0 if LAW361 <8
replace deseg_correct = 1 if LAW361== 2
replace deseg_correct = .5 if LAW361a== 2
replace deseg_correct = -1 if LAW361 == 1
replace deseg_correct = -.5 if LAW361a== 1

gen deseg_correct_flat = .
replace deseg_correct_flat = 0 if deseg_correct <0
replace deseg_correct_flat = 1 if deseg_correct >0

gen deseg_realagree_flat = deseg_realagree
recode deseg_realagree_flat (0 = .) (-1 = 0)

gen deseg_Ryes = .
replace deseg_Ryes = 0 if LAW362==2
replace deseg_Ryes = 1 if LAW362==1

gen deseg_SCyes = .
replace deseg_SCyes = 0 if LAW361==2
replace deseg_SCyes = 1 if LAW361==1
replace deseg_SCyes = .75 if LAW361a ==1
replace deseg_SCyes = .25 if LAW361a ==2

/* Handguns */

gen guns_agree = 1 if (LAW341==1 & LAW371==2)|(LAW341==4 & LAW371==1)
replace guns_agree = 1 if (LAW341==2 & LAW371==2)|(LAW341==3 & LAW371==1)

replace guns_agree = -1 if (LAW341==4 & LAW371==2)|(LAW341==1 & LAW371==1)
replace guns_agree = -1 if (LAW341==3 & LAW371==2)|(LAW341==2 & LAW371==1)

replace guns_agree = .5 if (LAW341==1 & LAW371a==2)|(LAW341==3 & LAW371a==1)
replace guns_agree = .5 if (LAW341==2 & LAW371a==2)|(LAW341==4 & LAW371a==1)

replace guns_agree = -.5 if (LAW341==3 & LAW371a==2)|(LAW341==1 & LAW371a==1)
replace guns_agree = -.5 if (LAW341==4 & LAW371a==2)|(LAW341==2 & LAW371a==1)

replace guns_agree = . if LAW341==.|LAW371==.
replace guns_agree = 0 if LAW371a==3

gen guns_notsure= LAW371==3 if LAW371!=.

gen guns_agree_flat = 0 
replace guns_agree_flat = 1 if guns_agree >0

gen guns_agree_01 = guns_agree
recode guns_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

gen guns_realagree = 1 if LAW341==2 | LAW341 == 1
replace guns_realagree = -1 if LAW341== 3 | LAW341==4
replace guns_realagree = 0 if LAW341==8

gen guns_correct = 0 if LAW361 <8
replace guns_correct = 1 if LAW371== 2
replace guns_correct = .5 if LAW371a== 2
replace guns_correct = -1 if LAW371 == 1
replace guns_correct = -.5 if LAW371a== 1

gen guns_correct_flat = .
replace guns_correct_flat = 0 if guns_correct <0
replace guns_correct_flat = 1 if guns_correct >0

gen guns_realagree_flat = guns_realagree
recode guns_realagree_flat (0 = .) (-1 = 0)

gen guns_Ryes = .
replace guns_Ryes = 0 if LAW341==3 | LAW341==4
replace guns_Ryes = 1 if LAW341==1 | LAW341==2

gen guns_SCyes = .
replace guns_SCyes = 0 if LAW371==2
replace guns_SCyes = 1 if LAW371==1
replace guns_SCyes = .75 if LAW371a ==1
replace guns_SCyes = .25 if LAW371a ==2


/* Roe */

gen roe_agree = 1 if LAW342==1
replace roe_agree = -1 if LAW342==2
replace roe_agree = . if LAW342==8

gen roe_agree_flat = 0 
replace roe_agree_flat = 1 if roe_agree >0

gen roe_agree_01 = roe_agree
recode roe_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

/* Illegal Immigration - allow arrests without warrant */

gen arrest_agree = 1 if (LAW347_1==1 & LAW370_1==1) | (LAW347_1==2 & LAW370_1==2) 
replace arrest_agree = -1 if (LAW347_1==2 & LAW370_1==1) | (LAW347_1==1 & LAW370_1==2)
replace arrest_agree = 0 if LAW370_1==8

gen arrest_notsure = 1 if LAW370_1==3

gen arrest_agree_flat = 0 
replace arrest_agree_flat = 1 if arrest_agree >0

gen arrest_agree_01 = arrest_agree
recode arrest_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)


/* Illegal Immigration - no documentation misdemeanor */

gen crime_agree = 1 if (LAW347_2==1 & LAW370_2==1) | (LAW347_2==2 & LAW370_2==2) 
replace crime_agree = -1 if (LAW347_2==2 & LAW370_2==1) | (LAW347_2==1 & LAW370_2==2)
replace crime_agree = 0 if LAW370_2==8

gen crime_notsure = 1 if LAW370_2==3

gen crime_agree_flat = 0 
replace crime_agree_flat = 1 if crime_agree >0

gen crime_agree_01 = crime_agree
recode crime_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)

/* Illegal Immigration - federal work permit */

gen permit_agree = 1 if (LAW347_3==1 & LAW370_3==1) | (LAW347_3==2 & LAW370_3==2) 
replace permit_agree = -1 if (LAW347_3==2 & LAW370_3==1) | (LAW347_3==1 & LAW370_3==2)
replace permit_agree = 0 if LAW370_3==8

gen permit_notsure = 1 if LAW370_3==3

gen permit_agree_flat = 0 
replace permit_agree_flat = 1 if permit_agree >0

gen permit_agree_01 = permit_agree
recode permit_agree_01 (-1=0) (-.5 = .25) (0=.5) (.5 = .75) (1=1)


egen agree_score_12 = rmean(guns_agree pb_agree mandate_agree medicaid_agree cu_agree deseg_agree arrest_agree crime_agree permit_agree)

egen agreescore12b = rmean(guns_agree pb_agree mandate_agree medicaid_agree cu_agree deseg_agree)
egen agreescore12_flat = rmean(guns_agree_flat pb_agree_flat mandate_agree_flat medicaid_agree_flat cu_agree_flat deseg_agree_flat arrest_agree_flat crime_agree_flat permit_agree_flat)
egen agreescore12b_flat = rmean(guns_agree_flat pb_agree_flat mandate_agree_flat medicaid_agree_flat cu_agree_flat deseg_agree_flat)

egen agreescore12b_sum = rsum(guns_agree pb_agree mandate_agree medicaid_agree cu_agree deseg_agree)

egen agreescore12b_01 = rmean(guns_agree_01 pb_agree_01 mandate_agree_01 medicaid_agree_01 cu_agree_01 deseg_agree_01)

egen issuesunsure = rsum(guns_notsure pb_notsure mandate_notsure medicaid_notsure cu_notsure deseg_notsure arrest_notsure crime_notsure permit_notsure)
egen issuesunsureb = rsum(guns_notsure pb_notsure mandate_notsure medicaid_notsure cu_notsure deseg_notsure)
replace issuesunsureb = issuesunsureb/6 //to line up with ns_score in earlier years.

egen agreescore12b_actualagree = rmean(guns_realagree pb_realagree mandate_realagree medicaid_realagree cu_realagree deseg_realagree)

egen realagree_flat = rmean(guns_realagree_flat pb_realagree_flat mandate_realagree_flat medicaid_realagree_flat cu_realagree_flat deseg_realagree_flat)


/* Party */

gen SC_party = 0
replace SC_party = 1 if LAW311==2
replace SC_party = -1 if LAW311==1
gen SC_party_dk = SC_party==0

gen pid = 0 if pid7<7 & pid7!=.
replace pid = 1 if pid7>=5&pid7<=7
replace pid = -1 if pid7>=1&pid7<=3
gen indie=pid==0

gen SC_samepid = SC_party*pid
gen SC_partyunsure = 0
replace SC_partyunsure = 1 if LAW311==4 

gen SC_samepid_real = 0
replace SC_samepid_real = 1 if pid == 1
replace SC_samepid_real = -1 if pid == -1

pwcorr SC_samepid SC_samepid_real

gen ideology7pt=.
replace ideology7pt = CC334A if CC334A <8

gen SC_partycorrect_flat = 0
replace SC_partycorrect_flat = 1 if LAW311==2

gen SC_partyrealagree = 0
replace SC_partyrealagree = 1 if pid ==1

gen SC_partypagree = 0
replace SC_partypagree =1 if (pid == 1 & LAW311==2) | (pid == -1 & LAW311==1)

/* Approval */
gen SC_A_2012 = 5-LAW380 if LAW380!=8
gen SC_A_d_2012 = 1 if SC_A_2012==4 | SC_A_2012==3
replace SC_A_d_2012 = 0 if SC_A_2012==1 | SC_A_2012==2

gen SC_A_2012_alt = LAW380 if LAW380!=8
recode SC_A_2012_alt (1 = 1.5) (2 = .5) (3 = -.5) (4 = -1.5)
tab SC_A_2012_alt 

gen cong_A_2012 = 5-CC308b if CC308b!=8 & CC308b!=5
gen cong_A_d_2012 = 1 if cong_A_2012 ==4 |cong_A_2012 ==3
replace cong_A_d_2012 = 0 if cong_A_2012 ==1 |cong_A_2012 ==2

gen pres_A_2012 = 5-CC308a if CC308a!=8 & CC308a!=5
gen pres_A_d_2012 = 1 if pres_A_2012 ==4 |pres_A_2012 ==3
replace pres_A_d_2012 = 0 if pres_A_2012 ==1 |pres_A_2012 ==2

gen congdiff  = SC_A_2012 - cong_A_2012  if SC_A_2012!=. & cong_A_2012 !=. //alternative outcome var.
tab congdiff


/* Correctness */
egen correctscore12 = rsum(pb_correct mandate_correct medicaid_correct cu_correct deseg_correct guns_correct)
tab correctscore12
svy:tab correctscore12
//look at any right answers (even if after the not-sure prompt)
egen correctscore12_flat = rsum(pb_correct_flat mandate_correct_flat medicaid_correct_flat cu_correct_flat deseg_correct_flat guns_correct_flat)
tab correctscore12_flat
svy:tab correctscore12_flat

gen full4plus = 0
replace full4plus = 1 if correctscore12_flat >=4
gen full5plus = 0
replace full5plus = 1 if correctscore12_flat >=5
gen full6plus = 0
replace full6plus = 1 if correctscore12_flat ==6

gen fullminus2 = 0
replace fullminus2 = 1 if correctscore12_flat <= -2

gen fullminus3 = 0
replace fullminus3 = 1 if correctscore12_flat <= -3


//////////////////////////////////////////////////////////////
// FIRST, REPLICATE SUMMARY TABLES
//////////////////////////////////////////////////////////////
svyset [pweight=V102]

//some general sample questions for in-text discussion
svy:tab pid7
svy:tab LAW311
svy:tab agreescore12b
svy:tab agreescore12_flat
svy:tab agreescore12b_flat
svy:tab agreescore12b_sum

svy:tab pid SC_party

// Table 1 Perception of Court Decisions on Recent Cases

//with thanks to: http://thedatamonkey.blogspot.com/2011/01/stata-matrices.html
matrix perceptions = J(9, 13, .)
matrix rownames perceptions = LAW365 LAW366 LAW367 LAW368 LAW361a LAW371 LAW370_1 LAW370_2 LAW370_3
matrix colnames perceptions = YES NO NOTSURE SKIPPED ALLNOTSURE PROBABLYYES PROBABLYNO STILLNOTSURE SKIPPED2 FULLYES FULLNO ALLNOTSURE2 N
scalar counter = 0

foreach x of varlist LAW365 LAW366 LAW367 LAW368 LAW361 LAW371 { //(create loop here)
svy:tab `x' 
scalar counter = counter+1
//matrix list e(b)
matrix define tabres = e(b)
matrix define obs=e(N)
//display tabres[1,2]
//matrix perceptions[LAW365, 1]= tabres[1,1]
matrix perceptions[counter, 1]=tabres[1,1]
matrix perceptions[counter, 2]=tabres[1,2]
matrix perceptions[counter, 3]=tabres[1,3]
matrix perceptions[counter, 4]=tabres[1,4]
matrix perceptions[counter, 5]=tabres[1,3] + tabres[1,4]
matrix perceptions[counter, 13]=obs[1,1]

}


foreach x of varlist LAW370_1 LAW370_2 LAW370_3  { //(deal with imm. questions differently)
svy:tab `x' 
scalar counter = counter+1
matrix define tabres = e(b)
matrix perceptions[counter, 1]=tabres[1,1]
matrix perceptions[counter, 2]=tabres[1,2]
matrix perceptions[counter, 4]=tabres[1,3] //there's no notsure, just skipped
matrix perceptions[counter, 5]=tabres[1,3]

}

matlist perceptions
//now go back and fill in the not-sure "probably" responses.
scalar counter = 0

foreach x of varlist LAW365a LAW366a LAW367a LAW368a LAW361a LAW371a { 
svy:tab `x' 
scalar counter = counter+1
//matrix list e(b)
matrix define tabres = e(b)
//display tabres[1,2]
//matrix perceptions[LAW365, 1]= tabres[1,1]
matrix perceptions[counter, 6]=tabres[1,1]
matrix perceptions[counter, 7]=tabres[1,2]
matrix perceptions[counter, 8]=tabres[1,3]
matrix perceptions[counter, 9]=tabres[1,4]
matrix perceptions[counter, 10]=perceptions[counter,1] + perceptions[counter,6] //all yeses 
matrix perceptions[counter, 11]=perceptions[counter,2] + perceptions[counter,7]  //all no 
matrix perceptions[counter, 12]=perceptions[counter,8] +perceptions[counter,9] //the still-not-sure plus the skips
}

matlist perceptions


//Table 2 Public Preferences on Recent Cases

matrix preferences = J(6, 3, .)
matrix rownames preferences = LAW334 LAW346_1 LAW346_3 LAW335 LAW362 LAW341
matrix colnames preferences = YES NO N
scalar counter = 0

foreach x of varlist LAW334 LAW346_1 LAW346_3 LAW335 LAW362  { 
tab `x'
} //quick peek at how many skipped. generally less than 1%, never 3%, regardless of whether or not using svy weights. note this for R2.


foreach x of varlist LAW334 LAW346_1 LAW346_3 LAW335 LAW362  { //(create loop here)
svy:tab `x' if `x' !=8 //am now dropping the few people who don't answer about their own prefs.
scalar counter = counter+1
//matrix list e(b)
matrix define tabres = e(b)
matrix define obs=e(N)
//display tabres[1,2]
//matrix perceptions[LAW365, 1]= tabres[1,1]
matrix preferences[counter, 1]=tabres[1,1]
matrix preferences[counter, 2]=tabres[1,2]
matrix preferences[counter, 3]=obs[1,1]

//matrix preferences[counter, 3]=tabres[1,3]
}

svy:tab LAW341 if LAW341!=8 //need to collapse categories for this one.
matrix define tabres = e(b)
matrix define obs=e(N)
display tabres[1,2]
scalar counter = counter+1
matrix preferences[counter, 1]=tabres[1,1]+tabres[1,2]
matrix preferences[counter, 2]=tabres[1,3]+tabres[1,4]
matrix preferences[counter, 3]=obs[1,1]

//matrix preferences[counter, 3]=tabres[1,5]

matrix list preferences


//now, remake for R2 with notsures included. 
matrix preferencesns = J(6, 3, .)
matrix rownames preferencesns = LAW334 LAW346_1 LAW346_3 LAW335 LAW362 LAW341
matrix colnames preferencesns = YES NO REFUSED
scalar counter = 0

foreach x of varlist LAW334 LAW346_1 LAW346_3 LAW335 LAW362  { //(create loop here)
svy:tab `x' //if `x' !=8 //am now dropping the few people who don't answer about their own prefs.
scalar counter = counter+1
//matrix list e(b)
matrix define tabres = e(b)
//display tabres[1,2]
//matrix perceptions[LAW365, 1]= tabres[1,1]
matrix preferencesns[counter, 1]=tabres[1,1]
matrix preferencesns[counter, 2]=tabres[1,2]
matrix preferencesns[counter, 3]=tabres[1,3]
}

svy:tab LAW341 //if LAW341!=8 //need to collapse categories for this one.
matrix define tabres = e(b)
display tabres[1,2]
scalar counter = counter+1
matrix preferencesns[counter, 1]=tabres[1,1]+tabres[1,2]
matrix preferencesns[counter, 2]=tabres[1,3]+tabres[1,4]
matrix preferencesns[counter, 3]=tabres[1,5]

matrix list preferencesns


/////////////////////////////////////////////////////////////////////////////////////////////
// Table 3 Regression Analysis: Approval of and Confidence in Supreme court as a 
//Function of Issue Agreement, Party Agreement, and Other Factors

//first, generate other variables: 
gen age = 2012-birthyr
gen agecat = 1 if age < 25
replace agecat = 2 if age >= 25 & age <= 34
replace agecat = 3 if age >= 35 & age <= 44
replace agecat = 4 if age >= 45 & age <= 54
replace agecat = 5 if age >= 55 & age <= 64
replace agecat = 6 if age >= 65 & age <= 74
replace agecat = 7 if age >= 75

gen educ4 = educ
recode educ4 (1=1) (2=2) (3=3) (4=3) (5=4) (6=4)

gen income=.
replace income = faminc if faminc <90 
replace pid7 =. if pid7>7
gen originalism = 1 if LAW422a== 1
replace originalism = 0 if LAW422a== 2

//need to append regressions to the other table
label var agreescore12b "Agreement score on cases"
label var issuesunsureb "'Not sure' score on cases"
label var SC_samepid "Same party as SCOTUS (perceived)"
label var SC_partyunsure "'Not sure' of SCOTUS' partisanship"
label var ideology7pt "Ideology (7-pt)"
label var pid7 "Party ID (7-pt)"
label var pid "Party ID (3-pt)"
label var originalism "Originalism"
label var pres_A_2012 "Presidential approval"
label var cong_A_2012 "Congressional approval"
label var income "Household Income"
label var gender "Gender"
label var agecat "Age (7 categories)"
label var educ4 "Education (4 categories)"
label var roe_agree "Agree with Roe v. Wade decision"
label var know_party_12 "Knowledge: which party appointed most justices"
label var know_chief_12 "Knowledge: Chief Justice"

outreg, clear
reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure know_party_12 know_chief_12 pid ideology7pt originalism roe_agree cong_A_2012 pres_A_2012 educ4 agecat gender  income [pweight= V102]
outreg using replicatingtable3_both.doc, se varlabels merge /*final column of Table 4 in main paper*/

//make ologit table.
outreg, clear
ologit SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure know_party_12 know_chief_12 pid ideology7pt originalism roe_agree cong_A_2012 pres_A_2012 educ4 agecat gender  income [pweight= V102]
outreg using ologittable2012.doc, se varlabels replace 

//bivariate
outreg, clear

svy: reg SC_A_2012 agreescore12b issuesunsureb 
outreg using regs_alt2012.doc, se varlabels replace

//drop cong/pres approval measures
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt income
outreg using regs_alt2012.doc, se varlabels merge

//drop Roe
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism know_party_12 know_chief_12 gender agecat educ4 pid ideology7pt cong_A_2012 pres_A_2012 income
outreg using regs_alt2012.doc, se varlabels merge

//preferred specification, but with objective issue agreement:
svy: reg SC_A_2012 agreescore12b_actualagree issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income

//run the whole thing without weights
reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg using regs_alt2012.doc, se varlabels merge

//run the whole thing without weights
reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_unweighted.doc, nolabel append 

//is everything being driven by one or two cases?
svy:reg SC_A_2012 pb_agree mandate_agree medicaid_agree cu_agree deseg_agree roe_agree arrest_agree crime_agree permit_agree guns_agree issuesunsure SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_sepcases.doc, nolabel replace


//also output the matrices from above
mat2txt, matrix(perceptions) saving(SCOTUSperceptions.xls) title(Perceptions of Supreme Court Opinions) replace
mat2txt, matrix(preferences) saving(SCOTUSpreferences.xls) title(Personal Views of Supreme Court Opinions) replace
mat2txt, matrix(preferencesns) saving(SCOTUSpreferences_notsures.xls) title(Personal Views of Supreme Court Opinions) replace

//look at party:
//start with regular preferred specification:
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg using regs_party2012.doc, se varlabels replace

//Republicans
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4  ideology7pt cong_A_2012 pres_A_2012 income if pid==1
outreg using regs_party2012.doc, se varlabels merge

//Democrats
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4  ideology7pt cong_A_2012 pres_A_2012 income if pid==-1
outreg using regs_party2012.doc, se varlabels merge


//10/17/2013: look at right/wrong people.
//preferred specification:
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_rw.doc, replace

//preferred specification, but with objective issue agreement added:
svy: reg SC_A_2012 agreescore12b agreescore12b_actualagree issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_rw.doc,  append

//preferred specification, for only the correct people:
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if full6==1
outreg2 using regs_rw.doc, append
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if full5plus==1
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if full4plus==1



//oh, use the same metric as 2009/2010-- knowledge of court partisanship.
//start with regular preferred specification:
svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg using regs_knowledge2012party.doc, varlabels replace

svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if SC_partycorrect_flat==0
outreg using regs_knowledge2012party.doc, varlabels merge

svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if SC_partycorrect_flat==1
outreg using regs_knowledge2012party.doc, varlabels merge

//also use metric of whether they got more than half of cases right?  and whether they know chief justice.
gen correctscore12_flat_avg = correctscore12_flat/6

svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12   gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg using regs_knowledge2012issues.doc, se varlabels replace

svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if correctscore12_flat_avg <=.5
outreg using regs_knowledge2012issues.doc, se varlabels merge

svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12  gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if correctscore12_flat_avg >.5
outreg using regs_knowledge2012issues.doc, se varlabels merge



svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12  know_chief_12  gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg using regs_knowledge2012chief.doc, se varlabels replace

svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if know_chief_12==0
outreg using regs_knowledge2012chief.doc, se varlabels merge

svy: reg SC_A_2012 agreescore12b issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12  gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income if know_chief_12==1
outreg using regs_knowledge2012chief.doc, se varlabels merge

//interaction
svy: reg SC_A_2012 agreescore12b c.agreescore12b#c.know_chief_12 know_chief_12 issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12  gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income


//also look at all of the variations of party/issue agreement.
//preferred specification, but with objective issue agreement added:
svy: reg SC_A_2012 agreescore12b agreescore12b_actualagree issuesunsureb SC_samepid SC_samepid_real SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_reality.doc,  replace

//and now add in individual cases instead of the actual agreement score.
svy: reg SC_A_2012 pb_realagree mandate_realagree medicaid_realagree cu_realagree deseg_realagree guns_realagree issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_reality.doc,  append

svy: reg SC_A_2012 pb_realagree pb_correct mandate_realagree mandate_correct medicaid_realagree medicaid_correct cu_realagree cu_correct deseg_realagree deseg_correct guns_realagree guns_correct issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_reality.doc,  append
//and add perceived agreement back in, for comparison.
svy: reg SC_A_2012 pb_agree mandate_agree medicaid_agree cu_agree deseg_agree guns_agree issuesunsureb SC_samepid SC_partyunsure originalism roe_agree know_party_12 know_chief_12 gender agecat educ4 pid7 ideology7pt cong_A_2012 pres_A_2012 income
outreg2 using regs_reality.doc,  append
svy: reg SC_A_2012 pb_agree mandate_agree medicaid_agree cu_agree deseg_agree guns_agree issuesunsureb 
outreg2 using regs_reality.doc,  append

log off
log close

capture log using CCES_Law_2012_basicstats.log, replace


//pull out a few other basic stats:
tab SC_A_2012
tab LAW380

tab pres_A_2012
tab cong_A_2012

summ agreescore12b
summ SC_samepid

//check whether preferences/beliefs about court decisions are correlated
pwcorr pb_Ryes pb_SCyes
pwcorr mandate_Ryes mandate_SCyes
pwcorr medicaid_Ryes medicaid_SCyes
pwcorr cu_Ryes cu_SCyes
pwcorr deseg_Ryes deseg_SCyes
pwcorr guns_Ryes guns_SCyes

svy:reg deseg_Ryes deseg_SCyes
//look at high- and low-agreement separately
pwcorr pb_Ryes pb_SCyes if SC_A_2012==4
pwcorr mandate_Ryes mandate_SCyes if SC_A_2012==4
pwcorr medicaid_Ryes medicaid_SCyes if SC_A_2012==4
pwcorr cu_Ryes cu_SCyes if SC_A_2012==4
pwcorr deseg_Ryes deseg_SCyes if SC_A_2012==4
pwcorr guns_Ryes guns_SCyes if SC_A_2012==4

pwcorr pb_Ryes pb_SCyes if SC_A_2012==1
pwcorr mandate_Ryes mandate_SCyes if SC_A_2012==1
pwcorr medicaid_Ryes medicaid_SCyes if SC_A_2012==1
pwcorr cu_Ryes cu_SCyes if SC_A_2012==1
pwcorr deseg_Ryes deseg_SCyes if SC_A_2012==1
pwcorr guns_Ryes guns_SCyes if SC_A_2012==1

//also just look amongst the cases
pwcorr pb_agree mandate_agree medicaid_agree cu_agree deseg_agree guns_agree


log off
log close

//Also, look at the Gibson question: at least cross-sectionally, does our approval measure seem to predict legitimacy/diffuse support?
//LAW384: a range of proposals to reduce SCOTUS' power/independence.
gen L384_1_agree = 0
replace L384_1_agree = 1 if LAW384_1==1
replace L384_1_agree = . if LAW384_1==8
tab L384_1_agree

gen L384_2_agree = 0
replace L384_2_agree = 1 if LAW384_2==1
replace L384_2_agree = . if LAW384_2==8
tab L384_2_agree

gen L384_3_agree = 0
replace L384_3_agree = 1 if LAW384_3==1
replace L384_3_agree = . if LAW384_3==8
tab L384_3_agree

gen L384_4_agree = 0
replace L384_4_agree = 1 if LAW384_4==1
replace L384_4_agree = . if LAW384_4==8
tab L384_4_agree

gen L384_5_agree = 0
replace L384_5_agree = 1 if LAW384_5==1
replace L384_5_agree = . if LAW384_5==8
tab L384_5_agree

gen proposalsindex =  L384_1_agree +  L384_2_agree + L384_3_agree + L384_4_agree + L384_5_agree
tab proposalsindex

svy: reg proposalsindex SC_A_2012
//outreg using diffusesupport.doc, se varlabels replace
outreg, se varlabels 
svy: reg proposalsindex SC_A_2012 pid ideology7pt 
//outreg using diffusesupport.doc, se varlabels merge
outreg, se varlabels merge
reg proposalsindex SC_A_2012 pid ideology7pt SC_samepid SC_partyunsure know_party_12 know_chief_12  originalism roe_agree cong_A_2012 pres_A_2012 educ4 agecat gender  income 
outreg using diffusesupport.doc, se varlabels merge landscape


///////////////////////////////////////////////////////////////////////////////////
// tables on preferences/ perceptions by year.

//do this for 2012
//correctness on court
svy:tab correctscore12_flat_avg
svy:mean correctscore12_flat_avg
matrix list e(b)

// agreement with court
svy:mean agreescore12b_flat
mean agreescore12b_flat [pweight= V102]


//CREATE TABLE OF APPROVAL RATINGS
matrix approval = J(1, 7, .)
matrix rownames approval = responses2012
matrix colnames approval = SC_mean SC_se President_mean President_se Congress_mean Congress_se N
scalar counter = 0


foreach x of varlist SC_A_d_2012 pres_A_d_2012 cong_A_d_2012  { //(create loop here)
	svy:mean `x' //34%
	scalar counter = counter+1
	matrix define tabres = e(b)
	matrix V = e(V)             // <- get the variance-covariance matrix
	matrix Var = vecdiag(V)
	matrix list V
	matrix approval[1, counter*2 -1]=tabres[1,1]
	matrix approval[1, counter *2 ]= sqrt(V[1,1])
}
matrix approval[1, 7]= _N

//do the same thing for 2009, 2010 and read in here
matin4 approvalold using approval0910.mat
matrix approvalall = approvalold \ approval
mat2txt, matrix(approvalall) saving("approvaltable.csv") replace

//OKAY, NEXT CREATE TABLE OF KNOWLEDGE, PERCEIVED + REAL ISSUE CONGRUENCE

matrix issuecong = J(1, 6, .)
matrix rownames issuecong = responses2012
matrix colnames issuecong = CorrectSC_mean CorrectSC_se Realagree_mean Realagree_se Perceivedagree_mean Perceivedagree_se
scalar counter = 0


foreach x of varlist correctscore12_flat_avg realagree_flat agreescore12b_flat  { //(create loop here)
	svy:mean `x' //34%
	scalar counter = counter+1
	matrix define tabres = e(b)
	matrix V = e(V)             // <- get the variance-covariance matrix
	matrix Var = vecdiag(V)
	matrix list V
	matrix issuecong[1, counter*2 -1]=tabres[1,1]
	matrix issuecong[1, counter *2 ]= sqrt(V[1,1])
}
matrix list issuecong

//do the same thing for 2009, 2010 and read in here
matin4 issuecongold using issuecong0910.mat
matrix issuecongall = issuecongold \ issuecong
mat2txt, matrix(issuecongall) saving("issuecongtable.csv") replace
matrix list issuecongall


//PERCEIVED + REAL PARTISAN CONGRUENCE

matrix partycong = J(1, 6, .)
matrix rownames partycong = responses2012
matrix colnames partycong = CorrectSCparty_mean CorrectSCparty_se Realpartyagree_mean Realpartyagree_se Perceivedpartyagree_mean Perceivedpartyagree_se
scalar counter = 0


foreach x of varlist SC_partycorrect_flat SC_partyrealagree SC_partypagree  { //(create loop here)
	svy:mean `x' //34%
	scalar counter = counter+1
	matrix define tabres = e(b)
	matrix V = e(V)             // <- get the variance-covariance matrix
	matrix Var = vecdiag(V)
	matrix list V
	matrix partycong[1, counter*2 -1]=tabres[1,1]
	matrix partycong[1, counter *2 ]= sqrt(V[1,1])
}
matrix list partycong

//do the same thing for 2009, 2010 and read in here
matin4 partycongold using partycong0910.mat
matrix partycongall = partycongold \ partycong
mat2txt, matrix(partycongall) saving("partycongtable.csv") replace
matrix list partycongall
 //
 
svy:tab pb_correct_flat
svy:tab mandate_correct_flat
svy:tab medicaid_correct_flat
svy:tab cu_correct_flat
svy:tab deseg_correct_flat
svy:tab guns_correct_flat

//look at individual agreement
svy:tab guns_agree_flat 
svy:tab pb_agree_flat 
svy:tab mandate_agree_flat
svy:tab medicaid_agree_flat
svy:tab cu_agree_flat
svy:tab deseg_agree_flat



